var App = getApp();
var ApiUrl = App.data.ApiUrl;
var Common = require('../../../utils/common.js');
Page({
    data: {
        type: 0,
        array: [],
        listall: [],
        thisone: false,
        choosenid: -1,
    },

    onLoad: function (options) {
        getApp().getSaleMode()
        var page = this;
        // 方法嵌套方法，避免AJAX异步带来的展示顺序异常
        this.getCouponById(0, function () {
            page.getCouponById(1, function () {
                page.getCouponById(2);
            });
        });
        wx.setNavigationBarTitle({
            title: "我的优惠券"
        })

    },
    onShow() {
        getApp().getSaleMode()
    },
    /**
     * 优惠券点击跳转到对应产品
     * coupon_type:"2",allow_item_id:"32" 跳到商品详情
     * coupon_type:"1",allow_item_id:"32|64" 跳到商品列表,allow_item_id:"32" 跳到商品详情
     * info:优惠券类型_商品id
     */
    goDetail: function (e) {
        var info = e.currentTarget.id.split("_");
        var dtype = info[0]; //coupon_type
        var id = info[1]; //allow_item_id
        if (dtype == 1) {
            var intid = parseInt(id);

            /**如果当前的可用商品只有一个 && intid存在 */
            if (intid == id && intid) {
                wx.navigateTo({
                    url: '/pages/product/detail/detail?itemId=' + id
                })
            } else {
                wx.switchTab({
                    url: '/pages/product/index/index'
                })
            }
        } else {
            wx.navigateTo({
                url: '/pages/product/detail/detail?itemId=' + id
            })

        }
    },

    // 详情
    showIntro: function (e) {
        var id = e.currentTarget.id;
        var page = this;
        if (id == page.data.choosenid) {
            page.setData({
                choosenid: -1
            })
        } else {
            page.setData({
                choosenid: id
            })
        }
    },

    // 选项卡点击
    getCoupon: function (e) {
        var id = e.currentTarget.id;
        this.setData({
            type: id
        })
    },

    // 优惠券详情
    getCouponById: function (status, callback) {
        wx.showLoading({
            title: '数据加载中',
        })
        var page = this;
        Common.my_request({
            url: 'AppMaster/Coupon/myCoupon',
            data: {
                status: status + 1,
            },
            success: function (res) {
                wx.hideLoading()
                if (res.data.result == 0) {
                    var array = page.data.array;
                    var name;
                    var listall = page.data.listall;
                    array[status] = {};
                    var data = res.data.data;
                    if (data) {
                        array[status].count = data.count;
                        var list = data.list;
                        for (var i = 0; i < list.length; i++) {
                            var start_time = list[i].start_time;
                            var stime = page.getDate(start_time, "nomin");
                            list[i].start_time = stime;

                            var end_time = list[i].end_time;
                            var etime = page.getDate(end_time, "nomin");
                            list[i].end_time = etime;

                            var bind_time = list[i].bind_time;
                            var ptime = page.getDate(bind_time, "hasmin");
                            list[i].bind_time = ptime;

                            var coupon_amount = list[i].coupon_amount;
                            coupon_amount = coupon_amount.substring(0, coupon_amount.indexOf("."));
                            list[i].coupon_amount = coupon_amount;

                        }
                    } else {
                        array[status].count = 0;
                    }

                    switch (status) {
                        case 0:
                            name = "未使用";
                            break;
                        case 1:
                            name = "已使用";
                            break;
                        case 2:
                            name = "已过期";
                            break;
                        default:
                            break;
                    }
                    array[status].name = name;
                    page.setData({
                        array: array
                    })

                    //将三次接口获取的数据组装到listall里
                    listall[status] = list;
                    page.setData({
                        listall: listall,
                    })

                    // 判断有没有回调方法，有就执行
                    callback ? callback() : "";
                }
            }
        });
    },
    getDate: function (tm, ttype) {
        var date = new Date(tm * 1000),
            Y = date.getFullYear() + '-',
            M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-',
            D = date.getDate(),
            h = date.getHours() + ':',
            m = date.getMinutes() + ':',
            s = date.getSeconds();
        var tt = Y + M + D + ' ' + h + m + s;
        if (ttype == "nomin") {
            tt = Y + M + D;
        }
        return tt;
    },
    onPullDownRefresh: function () {
        wx.stopPullDownRefresh()
    }
})